#ifndef LGAIMAGEPROCESS_H
#define LGAIMAGEPROCESS_H
#include "Utils/singleton.h"
#include "HalconCpp.h"
#include <QtWidgets>
#include "lgapackagemanager.h"
#include "resultmanager.h"
#include <HDevThread.h>
#include "staticfunction.h"

class LgaImageProcess : public Singleton<LgaImageProcess>, QObject
{
public:
	//typedef struct _RUN_INFO
	//{
	//	QString operatorName;
	//	QString packageName;
	//	int chipNum;
	//	QString productNum;
	//	bool bVarify;
	//	bool bAnalyzeImage;
	//	int checkItems;
	//}RUN_INFO;


	enum FAIL_REASON
	{
		FAIL_WRONG_IMAGE,
	/*	FAIL_PAD_MISS,
		FAIL_PAD_TOO_SMALL,
		FAIL_PAD_DISCOLOR,*/
		FAIL_PAD_DAMAGE,
		FAIL_CAP_AREA,
		FAIL_CAP_NUM,
		FAIL_SUBSTRATE_AREA,
	};

	enum CRITICAL_REASON
	{
		CRITICAL_PAD_TOO_SMALL,
		CRITICAL_PAD_DISCOLOR,
		CRITICAL_SUBSTRATE_AREA,
	};

	typedef struct _CRITICAL_ENTITY {
		QPointF coordinate;
		CRITICAL_REASON reason;
		QString reasonDesc;
		QString parameterDesc;
		union _MyUnion
		{
			//	int cirularity;
			double areaRatio;
			double shadowRatio;
			int substrateArea;
			double circularity;
		}value;
	}CRITICAL_ENTITY;

	typedef struct _INSPECT_ENTITY {
		//int pin1State;
		QPointF coordinate;
		FAIL_REASON reason;
		QString reasonDesc;
		QString parameterDesc;
		union _MyUnion
		{
			double areaRatio;
			double shadowRatio;
			int substrateArea;
			double circularity;
		}value;
	}INSPECT_ENTITY;
	QList<INSPECT_ENTITY> m_listInspectEntity;
	//QList<CRITICAL_ENTITY> m_listCriticalEntity;
	RUN_INFO m_ri;

	//int m_iVerifyCount;
	//int m_iInspectCount;

	void SetWinHandleAndSavePath(QWidget *widget, QString path, QDateTime time, RUN_INFO ri)
	{
		m_path = path;
		m_widget = widget;
		m_time = time;
		m_ri = ri;
	}

	void SetBallImage(HalconCpp::HImage image, int index);
	void SetSubstrateImage(HalconCpp::HImage image, int index);
	void SetCapImage(HalconCpp::HImage image, int index);

	ResultManager::ERROR_TYPE GetInspectResult();
	void GenFullImageSp7();
	//void InspectPadsSP5(int index);
	//void InspectSubstrateSP5(int index);
	
	void ShowAndSave(BOOL bSave);

	int TeachingAnalyze(HalconCpp::HImage imgPad, HalconCpp::HImage imgCap);
	void ReleaseBuffer();
protected:
    friend class Singleton<LgaImageProcess>;
	LgaImageProcess();
private:

	//typedef struct _QUAR_ANALYSIZE
	//{
	//	int rowMove;
	//	int columnMove;
	//	HalconCpp::HRegion regionPads;
	//	HalconCpp::HImage imagePadPart;
	//	HalconCpp::HImage imageSubPart;
	//}QUAR_ANALYSIZE;

	QList<HalconCpp::HImage> m_imagePadList;
	QList<HalconCpp::HImage> m_imageSubList;
	QList<HalconCpp::HImage> m_imageCapList;

	//HalconCpp::HImage m_imagePad[4];
	//HalconCpp::HImage m_imageSub[4];
	//HalconCpp::HImage m_imageCap[2];

	HalconCpp::HImage m_imgResult;
	HalconCpp::HImage m_imgResultCap;
	HalconCpp::HImage m_imgResultPad;
	HalconCpp::HImage m_imgResultSub;

	//QUAR_ANALYSIZE m_qa[4];
    QWidget *m_widget;
    QString m_path;
	QDateTime m_time;

	HalconCpp::HRegion m_regionPads;
	HalconCpp::HRegion m_regionDamagePad;

	HalconCpp::HRegion m_regionCaps;
	HalconCpp::HRegion m_regionDamageCap;
	
	HalconCpp::HRegion m_regionSubstrate;
	HalconCpp::HRegion m_regionDamageSubstrate;


	LgaPackageManager::INSPECT_PARAM m_inspectParam;

	ResultManager::ERROR_TYPE m_curRes;
	QString m_imgType;
	LgaPackageManager::POD_INFO m_pod;
	double m_meanGray = 0;
	
	void SaveResultImage(BOOL bRes);

	void ShowAllResult();

	//void GetPadPart(int index);
	BOOL InspectCaps(double threshold, double min, double max, int filter, double dif);
	//void InspectCapsSP5();

	void InspectSubstrate();
	// void InspectCapsSP3();
	void InspectPads();

	BOOL InitParameter();
	
	//ResultManager::ERROR_TYPE GetInspectResultSP5();
	ResultManager::ERROR_TYPE GetInspectResultSP7();
	ResultManager::ERROR_TYPE GetInspectResultAM5();


	//QThread *m_pThread[4];

	//HalconCpp::HImage m_imageAddR, m_imageAddG, m_imageAddB, m_imageDumpR, m_imageDumpG, m_imageDumpB;
	
};

#endif // LGAIMAGEPROCESS_H
